package LeetCode;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/5/6.
//
//https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
public class 找出字符串中第一个匹配项的下标 {
    public static void main(String[] args) {
        System.out.println(strStr("sadbutsad", "sad"));
//        System.out.println(strStr("leetcode","leeto"));
    }

    public static int strStr(String haystack, String needle) {
        if(!haystack.contains(needle))return -1;
        int res = -1;
        for (int i = 0; i <= haystack.length()-needle.length(); i++) {
            if(haystack.charAt(i) == needle.charAt(0)){
                int temp = 0;
                boolean flag = true;
                while(temp < needle.length()){
                    if(haystack.charAt(i + temp) != needle.charAt(temp)){
                        flag = false;
                        break;
                    }
                    temp++;
                }
                if(flag){
                    res = i;
                    break;
                }
            }
        }
        return res;
    }
}
